﻿
Public Class frmPicSelect

    Public m_ModName As String = "frmPicSelect"

    Private Structure struPic
        Public picname As String
        Public picurl As String
        Public picvalue As String
        Public picstacknum As Integer
        Public iserror As Integer
    End Structure

    Public frmtype As String = ""
    Private picgroup() As struPic
    Private picindex As Integer = 1
    Private picsum As Integer = 0

    Private Sub frmPicSelect_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If (frmtype = "error") Then
            Me.lbError.Visible = True
        Else
            Me.lbError.Visible = False
        End If
    End Sub


    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub


    Private Sub btnSelect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelect.Click
        picindex = 1
        picsum = 0
        Dim sqlStr = "SELECT ID, pic_name, pic_url, line_code,stack_no,stack_num,pic_value,crt_date,is_error FROM t_PicInfo where line_code = '" & CStr(Me.cbLine.SelectedIndex + 1) & "' and stack_no=" & Me.nudStack.Value & " and classcode= '" & Format(Me.dtpDate.Value, "yyyyMMdd") & "-" & (Me.cbClass.SelectedIndex + 1) & "'"
        If (frmtype = "error") Then
            sqlStr = sqlStr + " and is_error=1"
        End If

        Dim datatable As DataTable = execSQLQuery(sqlStr)
        If (datatable.Rows.Count > 0) Then
            If (setPicGroup(datatable)) Then
                showPicInfo()
            End If
        Else
            MsgBox("您设置查询的条件没有结果集")
        End If
    End Sub

    Private Function setPicGroup(ByVal dt As DataTable) As Boolean
        Dim funName As String = "setPicGroup"
        Try
            ReDim picgroup(dt.Rows.Count - 1)
            picsum = dt.Rows.Count
            For i As Integer = 0 To dt.Rows.Count - 1
                picgroup(i).picname = dt.Rows(i).Item(1).ToString
                picgroup(i).picurl = dt.Rows(i).Item(2).ToString
                picgroup(i).picvalue = dt.Rows(i).Item(6).ToString
                picgroup(i).picstacknum = dt.Rows(i).Item(5)
                picgroup(i).iserror = dt.Rows(i).Item(8)
            Next
            Return True
        Catch ex As Exception
            ErrInfoHandle(m_ModName, funName, ex.Message, ex.Source)
        End Try
        Return False
    End Function

    Private Sub showPicInfo()
        Dim funName As String = "showPicInfo"
        Try
            Me.txtNowPicNum.Text = Me.picindex
            Me.pbShow.Image = New Bitmap(picgroup(Me.picindex - 1).picurl)
            Me.txtPicValue.Text = picgroup(Me.picindex - 1).picvalue
            Me.txtStackNum.Text = picgroup(Me.picindex - 1).picstacknum
            If (picgroup(Me.picindex - 1).iserror = 0) Then
                Me.PictureBox1.ImageLocation = Application.StartupPath & "/images/right.jpg"
            Else
                Me.PictureBox1.ImageLocation = Application.StartupPath & "/images/false.jpg"
            End If
        Catch ex As Exception
            ErrInfoHandle(m_ModName, funName, ex.Message, ex.Source)
        End Try
    End Sub

    Private Sub btnLastPic_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLastPic.Click
        If (Me.picindex = 1) Then
            MsgBox("这是本垛第一张")
            Exit Sub
        End If
        Me.picindex = Me.picindex - 1
        showPicInfo()
    End Sub

    Private Sub btnNextPic_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNextPic.Click
        If (Me.picindex = Me.picsum) Then
            MsgBox("这是本垛最后一张")
            Exit Sub
        End If
        Me.picindex = Me.picindex + 1
        showPicInfo()
    End Sub

End Class